﻿using System;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{
    public partial class Program
    {
        public static bool CreateDatabase(
            string pFileName,
            DateTime? pFirstMonday,
            string pSourceFileImportTemplates,
            DataFile.CopyThose pDefineThem)
        {
            if (!pFirstMonday.HasValue)
            {
                ChooseWeekForm = new ChooseWeek();
                if (ChooseWeekForm.ShowDialog() == DialogResult.OK)
                {
                    pFirstMonday = new DateTime?(ChooseWeekForm.GetChoice);
                }
                else
                {
                    return false;
                }
            }
            CurrentFile = new DataFile(pFirstMonday.Value);

            var _def = new Template(
                Template.DefaultHours,
                Template.DefaultName);

            CurrentFile._templates.Add(
                Template.DefaultName,
                _def);

            if (pSourceFileImportTemplates != String.Empty)
            {
                CurrentFile.ImportTemplates(pSourceFileImportTemplates, pDefineThem);
            }
            var fs = File.Create(pFileName);
            CurrentFile.SaveTo(fs);
            return true;
        }


        internal static Exception ReadDatabase(string pFileName)
        {
            var result = DataFile.LoadFrom(pFileName);
            if (result.Key != null)
            {
                return result.Key; // rethrowing the exception
            }
            CurrentFile = result.Value;
            CurrentFile.SetParentsAndWorkingHours();
            /* setting parents and working hours uses subroutines
                 * which make the CurrentFile "think" that it WAS modified.
                 * but we've only just loaded it, it hasn't been modified by a human.
                 * modifications were caused by re-generating redundant data
                 * which does not get saved, or serialized (to save disk space).
                 * so, while Modified is true at this point - it'pShift safe and right
                 * to override it by resetting it to false */
            CurrentFile.Modified = false;
            CurrentFile.OnLeavingModifiedEntity += CurrentFile_OnLeavingModifiedEntity;
            return null;
        }

        static bool CurrentFile_OnLeavingModifiedEntity()
        {
            var decision = MessageBox.Show(
                Resources.MESSAGEBOX_QUESTION_SaveFile_MESSAGE.Replace(
                "#",
                CurrentFile.FileNameNoPath),
                Resources.MESSAGEBOX_QUESTION_SaveFile_CAPTION,
                MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Question);
            if (decision == DialogResult.Cancel)
            {
                return false;
            }
            if (decision == DialogResult.Yes) // save changes
            {
                frmMain.SaveFile();
            }
            return true;
        }

        public static string GetDefaultPath
        {
            get
            {
                return String.Concat(
                    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                    "\\",
                    Resources.DEFAULT_FOLDER_Rotas);
            }
        }

        public static string GetDefaultFileName
        {
            get
            {
                return Settings.Default.ROTAS_DefaultFileName;
            }
        }

        public static string GetDefaultExtension
        {
            get
            {
                return Settings.Default.ROTAS_DefaultFileExtension;
            }
        }

        public static string GetFullDefaultPathAndFileName
        {
            get
            {
                return String.Concat(GetDefaultPath, @"\",
                    GetDefaultFileName, @".",
                    GetDefaultExtension);
            }
        }
    }
}
